package com.cabal.auth.controller;

import com.cabal.auth.dto.*;
import com.cabal.auth.service.ICabalAuthService;
import com.cabal.common.constants.SystemConstants;
import com.cabal.common.encrypt.AESUtils;
import com.cabal.common.util.HttpClientUtil;
import com.cabal.mall.service.CabalMallService;
import com.cabal.mall.service.CabalToolItemSuitService;
import com.cabal.module.security.SecurityAdminNoCheck;
import com.cabal.module.security.SecurityValidMd5SignCheck;
import com.cabal.auth.facade.CabalCommonFacade;
import com.cabal.auth.service.ICabalCommonService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.URLDecoder;
import java.util.Arrays;
import java.util.List;

@RestController
@Slf4j
public class CabalCommonController implements CabalCommonFacade {

    @Resource
    private ICabalCommonService commonService;

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Resource
    private CabalMallService cabalMallService;

    @Autowired
    private ICabalAuthService cabalAuthService;

    @Resource
    private CabalToolItemSuitService cabalToolItemSuitService;

    @Override
    @SecurityValidMd5SignCheck
    public String register(@RequestBody CabalUserInfoDTO cabalUserInfoDTO) {
        return commonService.register(cabalUserInfoDTO);
    }

    @Override
    @SecurityValidMd5SignCheck
    public String sendMessage(@RequestBody SendMessageDTO dto, HttpServletRequest request) {
        String ip = HttpClientUtil.getClientIp(request);
        log.info("客户端请求IP地址，注册账号申请>>>>{}", ip);
        return commonService.sendMessage(dto, ip);
    }

    @Override
    @SecurityValidMd5SignCheck
    public String checkAccount(@RequestBody CabalUserInfoDTO cabalUserInfoDTO) {
        return commonService.checkAccount(cabalUserInfoDTO);
    }

    @Override
    @SecurityAdminNoCheck
    public List<String> getAccountByUserName(@RequestParam("userName") String userName, HttpServletRequest request, HttpServletResponse response) {
        return commonService.getAccountByUserName(userName);
    }

    @Override
    @SecurityAdminNoCheck
    public String batchEncryptPhone(HttpServletRequest request) {
        String ip = HttpClientUtil.getClientIp(request);
        log.info("客户端请求IP地址>>>>{}", ip);
        if(!"127.0.0.1".equals(ip)) {
            return "非法请求IP";
        }
        return commonService.batchEncryptPhone();
    }

    @Override
    @SecurityAdminNoCheck
    public void getEncryptPhoneByPhone(@RequestParam("phone") String phone, HttpServletRequest request, HttpServletResponse response) {
        String ip = HttpClientUtil.getClientIp(request);
        log.info("客户端请求IP地址>>>>{}", ip);
        if(!"127.0.0.1".equals(ip)) {
            return;
        }

        try {
            response.setContentType("text/html;charset=utf-8");
            String result = AESUtils.encrypt(phone, AESUtils.ENCRY_KEY);
            response.getWriter().print(result);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    @SecurityAdminNoCheck
    public void getPhoneByEncryptPhone(@RequestParam("encryptPhone") String encryptPhone, HttpServletRequest request, HttpServletResponse response) {
        String ip = HttpClientUtil.getClientIp(request);
        log.info("客户端请求IP地址>>>>{}", ip);
        if(!"127.0.0.1".equals(ip)) {
            return;
        }

        try {
            response.setContentType("text/html;charset=utf-8");
            String result = AESUtils.decrypt(encryptPhone, AESUtils.ENCRY_KEY);
            response.getWriter().print(result);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    @SecurityAdminNoCheck
    public void deleteBlackList(String phone, HttpServletRequest request, HttpServletResponse response) {
        String ip = HttpClientUtil.getClientIp(request);
        log.info("客户端请求IP地址>>>>{}", ip);
        if(!"127.0.0.1".equals(ip)) {
            return;
        }

        try {
            log.info("删除黑户手机号>>>>{}", phone);
            stringRedisTemplate.delete(SystemConstants.CABAL_BLACK_PHONE_KEY.concat(phone));
            response.setContentType("text/html;charset=utf-8");
            String result = "SUCCESS";
            response.getWriter().print(result);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    @SecurityValidMd5SignCheck
    public String mallShopBuyItems(@RequestBody CabalMallBuyItemDTO cabalMallBuyItemDTO, HttpServletRequest request) {
        return cabalMallService.mallShopBuyItems(cabalMallBuyItemDTO);
    }

    @Override
    public String initMallItems() {
        String initData = "762|贤者护肩+3\n" +
                "763|战士护肩+4\n" +
                "764|贤者护肩+4\n" +
                "428|战士护肩+5\n" +
                "429|战士护肩+7\n" +
                "430|贤者护肩+5\n" +
                "431|贤者护肩+7\n" +
                "761|战士护肩+3";
        String[] itemList = initData.split("\n");
        List<String> items = Arrays.asList(itemList);
        cabalMallService.initMallItems(items, 8);
        return "SUCCESS";
    }

    @Override
    @SecurityValidMd5SignCheck
    public List<CabalRankInfoDTO> searchRankInfoList(@RequestBody CabalAuthDTO cabalAuthDTO, HttpServletRequest request) {
        String ip = HttpClientUtil.getClientIp(request);
        log.info("查询金币排行榜，客户端请求IP地址>>>>{}", ip);
        return cabalAuthService.searchRankInfoList(cabalAuthDTO);
    }

    @Override
    @SecurityValidMd5SignCheck
    public CabalMallAuthDTO getAccountInfo(@Param("userNum") String userNum, @Param("authKey") String authKey, HttpServletRequest request) {
        return cabalAuthService.getAccountInfo(userNum, authKey, request);
    }

    @Override
    @SecurityValidMd5SignCheck
    public List<MallBuyRecordDTO> searchMallBuyRecordList(@Param("userNum") String userNum) {
        return cabalMallService.searchMallBuyRecordList(userNum);
    }

    @Override
    @SecurityValidMd5SignCheck
    public List<MallSellItemDTO> searchMallSellItemList(@Param("category") String category) {
        return cabalMallService.searchMallSellItemList(category);
    }

    @Override
    @SecurityValidMd5SignCheck
    public List<CabalCharacterInfoDTO> searchCharacterList(@Param("account") String account) {
        return cabalAuthService.searchCharacterList(account);
    }

    @Override
    @SecurityValidMd5SignCheck
    public String getResultByEncryptPhoneOrDecryptPhone(@RequestParam("encryptPhone") String encryptPhone, @RequestParam("decryptPhone") String decryptPhone) {
        String result = "";
        if(StringUtils.isNotBlank(decryptPhone)) {
            try {
                result = AESUtils.encrypt(decryptPhone, AESUtils.ENCRY_KEY);
                return result;
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            result = AESUtils.decrypt(encryptPhone, AESUtils.ENCRY_KEY);
        }
        return result;
    }

    @Override
    @SecurityValidMd5SignCheck
    public String setTitleByCharacterIdx(@RequestParam("characterIdx") String characterIdx, @RequestParam("titleId") Integer titleId) {
        return cabalAuthService.setTitleByCharacterIdx(characterIdx, titleId);
    }

    @Override
    @SecurityValidMd5SignCheck
    public String unsealAccount(@RequestParam("account") String account) {
        return cabalAuthService.unsealAccount(account);
    }

    @Override
    @SecurityValidMd5SignCheck
    public String updateNation(@RequestBody CabalUpdateNationDTO cabalUpdateNationDTO) {
        return cabalAuthService.updateNation(cabalUpdateNationDTO);
    }

    @Override
    @SecurityValidMd5SignCheck
    public List<MallSellItemDTO> searchItemListByKeyword(@Param("itemNameKeyword") String itemNameKeyword) {
        return cabalMallService.searchItemListByKeyword(itemNameKeyword);
    }

    @Override
    @SecurityValidMd5SignCheck
    public String updateGatewayConfig(@RequestBody UpdateGatewayConfigDTO updateGatewayConfigDTO) {
        return cabalAuthService.updateGatewayConfig(updateGatewayConfigDTO);
    }

    @Override
    @SecurityValidMd5SignCheck
    public UpdateGatewayConfigDTO getUpdateGatewayConfigDTO() {
        return cabalAuthService.getUpdateGatewayConfigDTO();
    }

    @Override
    @SecurityValidMd5SignCheck
    public List<CabalCashBackPackDTO> searchCashBackPackList(@Param("account") String account) {
        return cabalAuthService.searchCashBackPackList(account);
    }

    @Override
    @SecurityValidMd5SignCheck
    public String updateCashBackPackDurationIdx(@Param("account") String account) {
        return cabalAuthService.updateCashBackPackDurationIdx(account);
    }

    @Override
    @SecurityValidMd5SignCheck
    public String delCharacter(@Param("characterIdx") String characterIdx) {
        return cabalAuthService.delCharacter(Integer.parseInt(characterIdx));
    }

    @Override
    @SecurityValidMd5SignCheck
    public String insertCabalToolItemSuit(@RequestBody CabalToolItemSuitDTO cabalToolItemSuitDTO) {
        try {
            cabalToolItemSuitDTO.setItemSuitName(URLDecoder.decode(cabalToolItemSuitDTO.getItemSuitName(), "UTF-8"));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return cabalToolItemSuitService.insertCabalToolItemSuit(cabalToolItemSuitDTO);
    }

    @Override
    @SecurityValidMd5SignCheck
    public String updateCabalToolItemSuit(@RequestBody CabalToolItemSuitDTO cabalToolItemSuitDTO) {
        try {
            cabalToolItemSuitDTO.setItemSuitName(URLDecoder.decode(cabalToolItemSuitDTO.getItemSuitName(), "UTF-8"));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return cabalToolItemSuitService.updateCabalToolItemSuit(cabalToolItemSuitDTO);
    }

    @Override
    @SecurityValidMd5SignCheck
    public String insertCabalToolItemScript(@RequestBody CabalToolItemScriptDTO cabalToolItemScriptDTO) {
        try {
            cabalToolItemScriptDTO.setItemName(URLDecoder.decode(cabalToolItemScriptDTO.getItemName(), "UTF-8"));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return cabalToolItemSuitService.insertCabalToolItemScript(cabalToolItemScriptDTO);
    }

    @Override
    @SecurityValidMd5SignCheck
    public String updateCabalToolItemScript(@RequestBody CabalToolItemScriptDTO cabalToolItemScriptDTO) {
        try {
            cabalToolItemScriptDTO.setItemName(URLDecoder.decode(cabalToolItemScriptDTO.getItemName(), "UTF-8"));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return cabalToolItemSuitService.updateCabalToolItemScript(cabalToolItemScriptDTO);
    }

    @Override
    @SecurityValidMd5SignCheck
    public List<CabalToolItemScriptDTO> selectCabalToolItemScriptList(@RequestParam("suitName") String suitName) {
        return cabalToolItemSuitService.selectCabalToolItemScriptList(suitName);
    }

    @Override
    @SecurityValidMd5SignCheck
    public String deleteCabalToolItemScriptById(@RequestParam("id") String id) {
        return cabalToolItemSuitService.deleteCabalToolItemScriptById(id);
    }

    @Override
    @SecurityValidMd5SignCheck
    public List<CabalToolItemSuitDTO> selectCabalToolItemSuitList() {
        return cabalToolItemSuitService.selectCabalToolItemSuitList();
    }

    @Override
    @SecurityValidMd5SignCheck
    public String deleteCabalToolItemSuitById(@RequestParam("id") String id) {
        return cabalToolItemSuitService.deleteCabalToolItemSuitById(id);
    }

    /**
     * 发送装备返回批次号，用于撤回操作
     *
     * @param cabalToolItemScriptDTO
     * @return
     */
    @Override
    @SecurityValidMd5SignCheck
    public String sendEquipment(@RequestBody CabalToolItemScriptDTO cabalToolItemScriptDTO) {
        return cabalToolItemSuitService.sendEquipment(cabalToolItemScriptDTO);
    }

    /**
     * 发送套装返回批次号，用于撤回操作
     *
     * @param cabalToolItemSuitDTO
     * @return
     */
    @Override
    @SecurityValidMd5SignCheck
    public String sendItemSuit(@RequestBody CabalToolItemSuitDTO cabalToolItemSuitDTO) {
        return cabalToolItemSuitService.sendItemSuit(cabalToolItemSuitDTO);
    }

    @Override
    @SecurityValidMd5SignCheck
    public String revokeSend(@RequestParam("tranNo") String tranNo) {
        return cabalToolItemSuitService.revokeSend(tranNo);
    }

    @Override
    @SecurityValidMd5SignCheck
    public String clearCashRecord(@RequestParam("account") String account) {
        return cabalAuthService.clearCashRecord(account);
    }

    @Override
    @SecurityValidMd5SignCheck
    public String copySuit(@RequestBody CabalToolItemSuitDTO cabalToolItemSuitDTO) {
        return cabalToolItemSuitService.copySuit(cabalToolItemSuitDTO);
    }

    @Override
    @SecurityValidMd5SignCheck
    public String updateVipLevel(@RequestParam("account") String account, @RequestParam("vipLevel") Integer vipLevel) {
        return cabalAuthService.updateVipLevel(account, vipLevel);
    }

    @Override
    @SecurityValidMd5SignCheck
    public List<CabalAvatarItemDTO> selectGarmentCallInfoList(@RequestParam("account") String account) {
        return cabalMallService.selectGarmentCallInfoList(account);
    }

    @Override
    @SecurityValidMd5SignCheck
    public List<WeaponAvatarItemDTO> selectWeaponCallInfoList(@RequestParam("account") String account) {
        return cabalMallService.selectWeaponCallInfoList(account);
    }

    @Override
    @SecurityValidMd5SignCheck
    public List<CabalAvatarItemDTO> selectHeadWearCallInfoList(@RequestParam("account") String account) {
        return cabalMallService.selectHeadWearCallInfoList(account);
    }

    @Override
    @SecurityValidMd5SignCheck
    public CabalLotteryPoolConfigDTO getLotteryPoolConfig(@Param("userNum") String userNum, @Param("authKey") String authKey, HttpServletRequest request) {
        return cabalMallService.getLotteryPoolConfig();
    }

    @Override
    @SecurityValidMd5SignCheck
    public List<CabalPrizeDrawLogDTO> searchCabalPrizeDrawLogList(@Param("userNum") String userNum) {
        return cabalMallService.searchCabalPrizeDrawLogList(userNum);
    }

    @Override
    @SecurityValidMd5SignCheck
    public List<PickUpItemDTO> selectPickUpItemList() {
        return cabalMallService.selectPickUpItemList();
    }


}
